#include "pch.hpp"
#include "XmlWriter.hpp"




DatabaseXmlWriter::DatabaseXmlWriter(const QString& fname, const LoggerPtr& logger)
	: m_logger(logger)
	, m_file(fname)
	, m_writer(&m_file)
	, m_xmlStream(m_writer)
{
	m_file.open(QIODevice::WriteOnly);
}
///////////////////////////////////////////////////////////////////////////////////////////////////

DatabaseXmlWriter::~DatabaseXmlWriter()
{}
///////////////////////////////////////////////////////////////////////////////////////////////////




void DatabaseXmlWriter::write(const STRICT_PTR(DatabaseItem)& item)
{
	m_xmlStream.openElement("item");
	m_xmlStream.writeAttribute("name", item->name());
	m_xmlStream.writeAttribute("type", item->type());

	QString inheritance = item->inheritance();
	if(!inheritance.isEmpty())
	{
		m_xmlStream.openElement("inherits");
		m_xmlStream.writeAttribute("item", inheritance);
		m_xmlStream.closeElement();
	}

	writeProperties(item->properties());

	m_xmlStream.closeElement();
}
///////////////////////////////////////////////////////////////////////////////////////////////////




DatabaseWriterPtr DatabaseXmlWriter::create(const QString& fname, const LoggerPtr& logger)
{
	return DatabaseWriterPtr(new DatabaseXmlWriter(fname, logger));
}
///////////////////////////////////////////////////////////////////////////////////////////////////

void DatabaseXmlWriter::writeProperties(const Properties& properties)
{
	foreach(const Properties::value_type& property, properties)
	{
		m_xmlStream.openElement("property");
		m_xmlStream.writeAttribute("name", property.first.c_str());
		m_xmlStream.writeAttribute("value", property.second.data().c_str());

		writeProperties(properties);

		m_xmlStream.closeElement();
	}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
